﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccessLayer;

namespace BusinessLogicLayer
{
    public class PhuCapBLL
    {
        QuanLyChamCongDataContext DB = new QuanLyChamCongDataContext();
        public List<PHUCAP> LayTatCa()
        {
            return DB.PHUCAPs.ToList();
        }

        public string KiemTraTonTai(int ma, string ten)
        {
            if (DB.PHUCAPs.Any(a => a.MaPhuCap == ma))
                return "Mã";
            else if (DB.PHUCAPs.Any(a => a.TenPhuCap == ten))
                return "Tên";
            else
                return null;
        }

        public bool KiemTraTenPhuCap(int ma, string ten)
        {
            if (DB.PHUCAPs.Any(a => a.MaPhuCap != ma && a.TenPhuCap == ten))
                return true;
            else
                return false;
        }

        public void Them(PHUCAP a)
        {
            DB.PHUCAPs.InsertOnSubmit(a);
            DB.SubmitChanges();
        }

        public void Sua(PHUCAP a)
        {
            PHUCAP b = DB.PHUCAPs.Single(x => x.MaPhuCap == a.MaPhuCap);
            b.TenPhuCap = a.TenPhuCap;
            b.TienPhuCap = a.TienPhuCap;
            b.MoTa = a.MoTa;
            DB.SubmitChanges();
        }

        public void Xoa(int ma)
        {
            PHUCAP p = DB.PHUCAPs.Single(a => a.MaPhuCap == ma);
            DB.PHUCAPs.DeleteOnSubmit(p);
            DB.SubmitChanges();
        }

        public PHUCAP LayPC(int ma)
        {
            PHUCAP pc = DB.PHUCAPs.SingleOrDefault(a => a.MaPhuCap == ma);
            return pc;
        }

        public List<int> LayPCNVThang(int manv, int thang, int nam)
        {
            List<int> ma = (from a in DB.CT_PHUCAPs
                            where a.MaNV == manv && a.Nam == nam && a.Thang == thang
                            select a.MaPhuCap).ToList();
            return ma;
        }
    }
}
